package com.ruoyi.course.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.course.utils.RoleUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.course.mapper.CdStudentTopicMapper;
import com.ruoyi.course.domain.CdStudentTopic;
import com.ruoyi.course.service.ICdStudentTopicService;

/**
 * 学生选课Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-20
 */
@Service
public class CdStudentTopicServiceImpl implements ICdStudentTopicService 
{
    @Autowired
    private CdStudentTopicMapper cdStudentTopicMapper;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private RoleUtils roleUtils;

    /**
     * 查询学生选课
     * 
     * @param studentId 学生选课主键
     * @return 学生选课
     */
    @Override
    public List<CdStudentTopic> selectCdStudentTopicByStudentId(Long studentId)
    {
        return cdStudentTopicMapper.selectCdStudentTopicByStudentId(studentId);
    }

    /**
     * 根据课题查询学生选题情况
     * @param topicId
     * @return
     */
    @Override
    public List<CdStudentTopic> selectCdStudentTopicByTopicId(Long topicId) {
        return cdStudentTopicMapper.selectCdStudentTopicByTopicId(topicId);
    }

    /**
     * 查询学生选课列表
     * 
     * @param cdStudentTopic 学生选课
     * @return 学生选课
     */
    @Override
    public List<CdStudentTopic> selectCdStudentTopicList(CdStudentTopic cdStudentTopic)
    {
        if (roleUtils.isStudent()) {
            // 学生只能查看自己的选题情况
            cdStudentTopic.setStudentId(SecurityUtils.getUserId());
        } else if (!roleUtils.isDirector() && roleUtils.isTeacher()) {
            // 教师（非系主任）可查看自己指导的题目
            cdStudentTopic.setTeacherId(SecurityUtils.getUserId());
        }
        List<CdStudentTopic> list = cdStudentTopicMapper.selectCdStudentTopicList(cdStudentTopic);
        for (CdStudentTopic studentTopic : list) {
            Long teacherId = studentTopic.getTeacherId();
            SysUser teacher = sysUserService.selectUserById(teacherId);
            studentTopic.setTeacherName(teacher.getNickName());

            Long studentId = studentTopic.getStudentId();
            SysUser student = sysUserService.selectUserById(studentId);
            studentTopic.setStudentName(student.getNickName());
        }

        return list;
    }

    /**
     * 新增学生选课
     * 
     * @param cdStudentTopic 学生选课
     * @return 结果
     */
    @Override
    public int insertCdStudentTopic(CdStudentTopic cdStudentTopic)
    {
        return cdStudentTopicMapper.insertCdStudentTopic(cdStudentTopic);
    }

    /**
     * 修改学生选课
     * 
     * @param cdStudentTopic 学生选课
     * @return 结果
     */
    @Override
    public int updateCdStudentTopic(CdStudentTopic cdStudentTopic)
    {
        return cdStudentTopicMapper.updateCdStudentTopic(cdStudentTopic);
    }

    /**
     * 批量删除学生选课
     * 
     * @param studentIds 需要删除的学生选课主键
     * @return 结果
     */
    @Override
    public int deleteCdStudentTopicByStudentIds(Long[] studentIds)
    {
        return cdStudentTopicMapper.deleteCdStudentTopicByStudentIds(studentIds);
    }

    /**
     * 删除学生选课信息
     * 
     * @param studentId 学生选课主键
     * @return 结果
     */
    @Override
    public int deleteCdStudentTopicByStudentId(Long studentId)
    {
        return cdStudentTopicMapper.deleteCdStudentTopicByStudentId(studentId);
    }
}
